package splider

import (
	"strings"

	"github.com/PuerkitoBio/goquery"
)

//分析获取列表内容
func ParseList(doc *goquery.Document) (dataList []map[string]string) {
	doc.Find(".job-list li").Each(func(i int, selection *goquery.Selection) {
		data := make(map[string]string)

		data["position"] = selection.Find(".job-title").Text()
		data["min_wages"], data["max_wages"] = getWages(selection)
		data["address"] = getAddress(selection)
		data["workingYear"] = getWorkYear(selection)
		data["education"] = getEducation(selection)
		data["company"] = selection.Find(".info-company .company-text .name").Text()
		data["company_people"] = getCompanyPeople(selection)
		data["financing_detail"] = getFinancingDetail(selection)
		data["created_time"] = strings.Replace(selection.Find(".info-publis p").Text(), "发布于", "", 1)
		data["url"], _ = selection.Find(".info-primary .name a").Attr("href")
		dataList = append(dataList, data)
	})

	return dataList
}

// 获取公司地址
func getAddress(selection *goquery.Selection) string {
	addressString, _ := selection.Find(".info-primary p").Html()

	return strings.Split(addressString, "<em class=\"vline\"></em>")[0]
}

//获取要求的工作时间
func getWorkYear(selection *goquery.Selection) string {
	addressString, _ := selection.Find(".info-primary p").Html()

	return strings.Split(addressString, "<em class=\"vline\"></em>")[1]
}

//获取需要的学历
func getEducation(selection *goquery.Selection) string {
	addressString, _ := selection.Find(".info-primary p").Html()

	return strings.Split(addressString, "<em class=\"vline\"></em>")[2]
}

//获取公司的人数
func getCompanyPeople(selection *goquery.Selection) (company_people string) {
	companyString, _ := selection.Find(".info-company .company-text p").Html()
	companySlice := strings.Split(companyString, "<em class=\"vline\"></em>")
	if len(companySlice) >= 3 {
		company_people = companySlice[2]
	} else {
		company_people = companySlice[1]
	}

	return company_people
}

//获取公司融资情况
func getFinancingDetail(selection *goquery.Selection) (financing_detail string) {
	companyString, _ := selection.Find(".info-company .company-text p").Html()
	companySlice := strings.Split(companyString, "<em class=\"vline\"></em>")
	if len(companySlice) >= 3 {
		financing_detail = companySlice[1]
	} else {
		financing_detail = ""
	}

	return financing_detail
}

//获取最高，最低的工资
func getWages(selection *goquery.Selection) (min_wages, max_wages string) {
	wages := strings.Split(selection.Find(".red").Text(), "-")
	return strings.Replace(wages[0], "k", "", 1), strings.Replace(wages[1], "k", "", 1)
}
